#include "rectangle.hpp"

Rectangle::Rectangle() : Plane(), size( 1.f ) { CacullateMinMax(); }

Rectangle::Rectangle( vec3f n, vec3f o, vec2f s ) : Plane( n, o ), size( s ) {
  CacullateMinMax();
}

// BUG: a sphere
bool Rectangle::Contains( const vec3f &point ) {
  bool contained = false;
  vec3f t = point - origin();
  vec3f forward = normalize( origin() );
  vec3f right = normalize( cross( forward, normal() ) );
  vec2f half = size / 2.f;
  bool tt =
      abs( dot( t, forward ) ) <= half.y && abs( dot( t, right ) ) <= half.x;
  bool isInPlane = dot( normal(), t ) == 0;
  bool isInSize = dot( MinVec() - point, MaxVec() - point ) <= 0 && tt;
  contained = isInPlane && isInSize;
  return contained;
}

bool Rectangle::Contains( const vec3f &o, const vec3f &point ) {
  bool contained = false;
  vec3f r = point - o;

  return contained;
}

bool Rectangle::hit( const Ray &r, f32 tmin, f32 tmax, f32 time,
                     HitRecord &record ) {
  bool hitted = false;
  vec3f col = record.color;
  hitted =
      __super::hit( r, tmin, tmax, time, record ) && Contains( r( record.t ) );
  if ( !hitted ) record.color = col;
  return hitted;
}

bool Rectangle::shadowHit( const Ray &r, f32 tmin, f32 tmax, f32 time ) {
  bool hitted = false;

  return false;
}